package com.example.demo.controller;

import com.example.demo.dto.CommentDto;
import com.example.demo.dto.DepartmentDto;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Department;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.RootMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired(required = false)
    private StudentMapper studentMapper;
    @Autowired(required = false)
    private CommentMapper commentMapper;

    @RequestMapping("myDepPage")
    public ResultUtil getMyDepList(HttpSession session,Integer page, Integer limit){
        Integer id= (Integer) session.getAttribute("id");
        page=(page-1)*limit;
        List<DepartmentDto> departmentDtos = studentMapper.selectMyDepPage(page, limit, id);
        Integer count = studentMapper.selectMyDepCount(id);
        System.out.println(departmentDtos);
        return new ResultUtil(0,"成功",count,departmentDtos);
    }

    @RequestMapping("depPage")
    public ResultUtil getDepList(Integer page, Integer limit){
        page=(page-1)*limit;
        List<DepartmentDto> departmentDtos = studentMapper.selectDepPage(page, limit);
        Integer count = studentMapper.selectDepCount();
        return new ResultUtil(0,"成功",count,departmentDtos);
    }

    //选课
    @RequestMapping("selectDep")
    public ResultUtil selectDep(HttpSession session,Integer depId){
        Integer sId = (Integer) session.getAttribute("id");
        //查询是否已经选过此课程
        Integer integer = studentMapper.selectDepIsSelect(sId, depId);
        if (integer>0){
            return new ResultUtil(500,"已经选择过此课程",null,null);
        }
        //没有选过，添加此课程
        Integer integer1 = studentMapper.insertDepSelect(sId, depId);
        if (integer1>0){
            //添加成功
            return new ResultUtil(200,"选课成功",null,null);
        }
        return new ResultUtil(500,"未知错误",null,null);
    }

    //取消选课
    @RequestMapping("clearDep")
    public ResultUtil clearDep(HttpSession session, Integer depId){
        Integer sId = (Integer) session.getAttribute("id");
        Integer integer = studentMapper.clearDep(sId, depId);
        if (integer>0){
            return new ResultUtil(200,"取消选课成功",null,null);
        }
        return new ResultUtil(500,"取消选课失败",null,null);
    }

    //我的评价页面
    @RequestMapping("myCommentPage")
    public ResultUtil myCommentPage(HttpSession session,Integer page,Integer limit){
        Integer sId = (Integer) session.getAttribute("id");
        page = (page-1)*limit;
        List<CommentDto> commentDtos = studentMapper.selectMyComment(page,limit,sId);
        Integer count = studentMapper.selectMyCommentCount(sId);
        return new ResultUtil(0,"查询成功",count,commentDtos);

    }

    //添加教师评价
    @RequestMapping("addComment")
    public ResultUtil addComment(HttpSession session,Comment comment){
        System.out.println(comment);
        Integer id = (Integer) session.getAttribute("id");
        //查询是否已经存在评价
        Integer integer = commentMapper.selectBySIdAndDepId(id, comment.getDepartmentId());
        if (integer > 0) {
            return new ResultUtil(500,"已经评价过此教师",null,null);
        }
        //添加评价
        comment.setStudentId(id);
        Integer integer1 = commentMapper.insertComment(comment);
        if (integer1>0){
            return new ResultUtil(200,"评价成功",null,null);
        }
        return new ResultUtil(500,"未知错误",null,null);
    }
    //修改教师评价
    @RequestMapping("editComment")
    public ResultUtil editComment(Integer id,String content,Integer score){
        Integer integer = studentMapper.updateComment(content, score, id);
        if (integer>0){
            return new ResultUtil(200,"修改评价成功",null,null);
        }
        return new ResultUtil(500,"修改评价失败",null,null);
    }

    //删除教师评价
    @RequestMapping("delComment")
    public ResultUtil delComment(Integer id,String content,Integer score){
        Integer integer = commentMapper.delComment(id);
        if (integer>0){
            return new ResultUtil(200,"删除评价成功",null,null);
        }
        return new ResultUtil(500,"删除评价失败",null,null);
    }

}
